using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NorthwindWeb.Models;

namespace NorthwindWeb.Controllers
{   
    public class EmployeeController : Controller
    {
		private readonly IEmployeeRepository employeeRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public EmployeeController() : this(new EmployeeRepository())
        {
        }

        public EmployeeController(IEmployeeRepository employeeRepository)
        {
			this.employeeRepository = employeeRepository;
        }

        //
        // GET: /Employee/

        public ViewResult Index()
        {
            return View(employeeRepository.AllIncluding(employee => employee.Employees1, employee => employee.Orders, employee => employee.Territories));
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "EmployeeID")]Employee employee)
        {
            if (ModelState.IsValid) {
                employeeRepository.InsertOrUpdate(employee);
                employeeRepository.Save();
                return RedirectToAction("Index");
            } else {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid) {
                employeeRepository.InsertOrUpdate(employee);
                employeeRepository.Save();
                return RedirectToAction("Index");
            } else {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            employeeRepository.Delete(id);
            employeeRepository.Save();

            return RedirectToAction("Index");
        }

        
        public JsonResult employeeGridData(int rows, int page)
        {
        	var count = employeeRepository.All.Count();
            var pageData = employeeRepository.All.OrderBy(x => x.EmployeeID).Skip((page - 1) * rows).Take(rows).Select(o => new {EmployeeID = o.EmployeeID, LastName = o.LastName, FirstName = o.FirstName, Title = o.Title, TitleOfCourtesy = o.TitleOfCourtesy, BirthDate = o.BirthDate, HireDate = o.HireDate, Address = o.Address, City = o.City, Region = o.Region, PostalCode = o.PostalCode, Country = o.Country, HomePhone = o.HomePhone, Extension = o.Extension, Notes = o.Notes, ReportsTo = o.ReportsTo, PhotoPath = o.PhotoPath});
            return Json(new { 
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
            }, JsonRequestBehavior.AllowGet);
        }
        
        
    }
}

